<template>
  <div class="app-container">
    <el-form :model="form" ref="form" label-width="100px" v-loading="formLoading" :rules="rules">
      <el-form-item label="年级：" prop="gradeLevel" required>
        <el-select v-model="form.gradeLevel" placeholder="年级"  @change="levelChange" clearable>
          <el-option v-for="item in levelEnum" :key="item.key" :value="item.key" :label="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="学科：">
        <el-select v-model="form.courseId" placeholder="学科" clearable> 
          <el-option v-for="item in subjectFilter" :key="item.courseId" :value="item.courseId" :label="item.courseName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="题干：" prop="questionName" required>
        <el-input v-model="form.questionName"   @focus="inputClick(form,'questionName')" />
      </el-form-item>
      <el-form-item label="解析：" prop="detaile" required>
        <el-input v-model="form.detail"  @focus="inputClick(form,'detail')" />
      </el-form-item>
      <el-form-item label="分数：" prop="score" required>
        <el-input-number v-model="form.score" :precision="1" :step="1" :max="100"></el-input-number>
      </el-form-item>
      <el-form-item label="难度：" required>
        <el-rate v-model="form.difficult" class="question-item-rate"></el-rate>
      </el-form-item>
      <el-form-item label="正确答案：" prop="answer" required>
        <el-input v-model="form.answer"   @focus="inputClick(form,'answer')" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button type="success" @click="updateForm">修改</el-button>
        <el-button @click="resetForm">重置</el-button>
        <!-- <el-button type="success" @click="questionItemAdd">添加选项</el-button> -->
        <el-button type="success" @click="showQuestion">预览</el-button>
      </el-form-item>
    </el-form>

    <el-dialog :visible.sync="questionShow.dialog" style="width: 100%;height: 100%">
      <QuestionShow :qType="questionShow.qType" :question="questionShow.question" :qLoading="questionShow.loading"/>
    </el-dialog>
  </div>
</template>

<script>
import QuestionShow from '@/components/Show'
import Ueditor from '@/components/Ueditor'
import { mapGetters, mapState, mapActions } from 'vuex'
import questionApi from '@/api/question'

export default {
  components: {
    Ueditor, QuestionShow
  },
  data () {
    return {
      form: {
        questionId: null,
        type: 3,
        gradeLevel: null,
        courseId: null,
        questionName: '',
        items: null,
        detail: '',
        answer: '',
        score: '',
        difficult: 0
      },
          levelEnum:[
       { key :'1',value:'一年级'},
      { key :'2',value:'二年级'},
      { key :'3',value:'三年级'},
      { key :'4',value:'四年级'},
      { key :'5',value:'大学年级'},
       ] ,
             subjectFilter: [ 
       { key :'1',value:'语文'},
      { key :'2',value:'数学'},
      { key :'3',value:'英语'},
      { key :'4',value:'java'},
      { key :'5',value:'c++'},
       ] ,
      formLoading: false,
      rules: {
        gradeLevel: [
          { required: false, message: '请选择年级', trigger: 'change' }
        ],
        subjectId: [
          { required: false, message: '请选择学科', trigger: 'change' }
        ],
        title: [
          { required: false, message: '请输入题干', trigger: 'blur' }
        ],
        analyze: [
          { required: false, message: '请输入解析', trigger: 'blur' }
        ],
        score: [
          { required: false, message: '请输入分数', trigger: 'blur' }
        ],
        correct: [
          { required: false, message: '请选择正确答案', trigger: 'blur' }
        ]
      },
      richEditor: {
        dialogVisible: false,
        object: null,
        parameterName: '',
        instance: null
      },
      questionShow: {
        qType: 0,
        dialog: false,
        question: null,
        loading: false
      }
    }
  },
  created () {
    let id = this.$route.query.id
    
    let _this = this
    if (id && parseInt(id) !== 0) {
    this.$http.post("question/id/?id="+id).then(
         re=>{
             const res = re
             this.form=res.data
             }
     );  
    this.form.id=id
    }
  },
  mounted() {
    this.getcourseId();
  },
  methods: {
    //zjx
    getcourseId(){
        this.$http.get("course/get").then(res =>{
          console.log("加载数据成功")
          this.subjectFilter=res.data;
          console.log(this.subjectFilter[1].courseId)
        })
      },
    async updateForm(){
      const { data: res } = await this.$http.post("/question/update",this.form);
      if(res != null){
        this.$message.success("修改成功")
      }else{
        this.$message.error("修改失败")
      }
    },
    async submitForm () {
        // alert('点击了提交按钮')
            // 调用post请求
            const { data: res } = await this.$http.post("/question/edit",this.form);
            if(res != null){
        this.$message.success("添加成功")
      }else{
        this.$message.error("添加失败")
      }
    },
    editorReady (instance) {
      this.richEditor.instance = instance
      let currentContent = this.richEditor.object[this.richEditor.parameterName]
      this.richEditor.instance.setContent(currentContent)
      // 光标定位到Ueditor
      this.richEditor.instance.focus(true)
    },
    inputClick (object, parameterName) {
      this.richEditor.object = object
      this.richEditor.parameterName = parameterName
      this.richEditor.dialogVisible = true
    },
    editorConfirm () {
      let content = this.richEditor.instance.getContent()
      this.richEditor.object[this.richEditor.parameterName] = content
      this.richEditor.dialogVisible = false
    },
    questionItemRemove (index) {
      this.form.items.splice(index, 1)
    },
    // questionItemAdd () {
    //   let items = this.form.items
    //   let newLastPrefix
    //   if (items.length > 0) {
    //     let last = items[items.length - 1]
    //     newLastPrefix = String.fromCharCode(last.prefix.charCodeAt() + 1)
    //   } else {
    //     newLastPrefix = 'A'
    //   }
    //   items.push({ id: null, prefix: newLastPrefix, content: '' })
    // },

    resetForm () {
      let lastId = this.form.id
      this.$refs['form'].resetFields()
      this.form = {
        questionId: null,
        questionType: 3,
        gradeLevel: null,
        courseId: null,
        title: '',
        items: '',
        questionId: '',
        answer: '',
        score: '',
        difficult: 0
      }
      this.form.id = lastId
    },
    levelChange () {
      this.form.subjectId = null
      this.subjectFilter = this.subjects.filter(data => data.level === this.form.gradeLevel)
    },
    showQuestion () {
      this.questionShow.dialog = true
      this.questionShow.qType = this.form.type
      this.questionShow.question = this.form
    },
   // ...mapActions('exam', { initSubject: 'initSubject' }),
    //...mapActions('tagsView', { delCurrentView: 'delCurrentView' })
  },
  // computed: {
  //   ...mapGetters('enumItem', ['enumFormat']),
  //   ...mapState('enumItem', {
  //     questionTypeEnum: state => state.exam.question.typeEnum,
  //     levelEnum: state => state.user.levelEnum
  //   }),
  //   ...mapState('exam', { subjects: state => state.subjects })
  // }
}
</script>
